<script setup lang='ts'>
import CustomNavbar from '@/pages/index/components/CustomNavbar.vue'
import XtxSwiper from '@/components/XtxSwiper.vue'
import XtxGuess from '@/components/XtxGuess.vue'
import { getHomeBannerAPI, getHomeCategoryAPI, getHomeHotAPI } from '@/services/home'
import { onLoad } from '@dcloudio/uni-app'
import { ref } from 'vue'
import type { BannerItem, CategoryItem, HotItem } from '@/types/home'
import CategoryPanel from './components/CategoryPanel.vue'
import HotPanel from './components/HotPanel.vue'
// import type { XtxGuessInstance } from '@/types/component'
import PageSkeleton from './components/PageSkeleton.vue'
import { useGuessList } from '@/composables'

const bannerList = ref<BannerItem[]>([])
const getHomeBannerData = async () => {
  const res = await getHomeBannerAPI()
  console.log('res：:', res)
  bannerList.value = res.result
}

// 获取前台分类数据接口函数
const categoryList = ref<CategoryItem[]>([])
const getHomeCategoryData = async () => {
  const res = await getHomeCategoryAPI()
  categoryList.value = res.result
}

// 获取热门推荐数据
const hotList = ref<HotItem[]>([])
const getHomeHotData = async () => {
  const res = await getHomeHotAPI()
  hotList.value = res.result
}

// 是否加载中的标记
const isLoading = ref(false)

// 页面加载
onLoad(async () => {
  isLoading.value = true
  // getHomeBannerData()
  // getHomeCategoryData()
  // getHomeHotData()
  await Promise.all([
    getHomeBannerData(),
    getHomeCategoryData(),
    getHomeHotData(),
  ])
  isLoading.value = false
})

// 获取猜你组件实例
// const guessRef = ref<XtxGuessInstance>()

// // 滚动触底
// const onScrollTolower = () => {
//   console.log('滚动了');
//   guessRef.value?.getMore()
// }
// 封装以上， 组合函数
const { guessRef, onScrolltolower } = useGuessList()

const isTriggered = ref(false)
// 自定义下拉刷新被触发  @refresherrefresh
const onRefresherrefresh = async () => {
  console.log('自定义下拉刷新被触发')
  // 标记开始动画
  isTriggered.value = true
  // 加载数据
  // await getHomeBannerData()
  // await getHomeCategoryData()
  // await getHomeHotData()
  // 重置数据
  guessRef.value?.resetData()
  // 同时发送数据
  await Promise.all([
    getHomeBannerData(),
    getHomeCategoryData(),
    getHomeHotData(),
    guessRef.value?.getMore(),
  ])
  // 关闭动画
  isTriggered.value = false
}
</script>

<template>
  <!--自定义导航栏-->
  <CustomNavbar></CustomNavbar>
  <!-- 滚动容器
      refresher-enabled：下拉刷新
      设置当前下拉刷新状态，true 表示下拉刷新已经被触发，false 表示下拉刷新未被触发
      refresher-triggered="false"
   -->
  <scroll-view refresher-enabled @refresherrefresh="onRefresherrefresh" :refresher-triggered="isTriggered" scroll-y
    class="scroll-view" @scrolltolower="onScrolltolower">
    <PageSkeleton v-if="isLoading" />
    <template v-else>
      <!--轮播图-->
      <XtxSwiper :list="bannerList"></XtxSwiper>
      <!-- 首页分类面板 -->
      <CategoryPanel :list="categoryList" />
      <!-- 热门推荐 -->
      <HotPanel :list="hotList" />
      <!-- 猜你喜欢 -->
      <XtxGuess ref="guessRef" />
    </template>
  </scroll-view>

</template>

<style lang='scss'>
page {
  display: flex;
  flex-direction: column;
  height: 100%;
  background-color: #f7f7f7;
}

.scroll-view {
  flex: 1;
}
</style>
